package untiring.beans;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.web.multipart.MultipartFile;

import untiring.interfaces.ProductDAO;
import untiring.models.GalleryVO;
import untiring.models.ProductVO;

@Repository("productDAO")
public class ProductDAOimpl implements ProductDAO {

	
	@Autowired
	private SqlSession sqlSession;
	
	@Override
	public boolean insertProduct(ProductVO p) {
		boolean result = false;
		if(sqlSession.insert("productMapper.insertProduct",processImage(p))>0 )result = true;
		return result;
	}

	@Override
	public List<ProductVO> selectProduct() {
		return sqlSession.selectList("productMapper.selectProduct");
	}

	@Override
	public ProductVO selectOneProduct(String pno) {
		return sqlSession.selectOne("productMapper.viewProduct",pno);
	}

	@Override
	public boolean updateProduct(ProductVO p) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean deleteProduct(String pno) {
		// TODO Auto-generated method stub
		return false;
	}
	
	public Map<String,Object> processImage(ProductVO p){
		Map<String,Object> sendData = new HashMap<>();
		sendData.put("pvo", p); //title, uid, contents ,uuid
		int cnt = 1;
		for(MultipartFile mf:p.getImg()){
			String fname = "";
			if(mf.getSize()>0)
				fname = mf.getOriginalFilename();
			sendData.put("img"+(cnt++), fname);
		} //for
		return sendData;
	}

	@Override
	public ProductVO selectImage(ProductVO p) {
		// TODO Auto-generated method stub
		return null;
	}

}
